package com.zhongwang.business.module.baseCostManage.service;


import com.zhongwang.business.constant.Constant;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.dao.WeightHoursDao;
import com.zhongwang.business.module.baseCostManage.model.WeightHoursModel;
import com.zhongwang.business.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *  服务层
 * @author wzy
 */
@Service
public class WeightHoursService {
    @Autowired
    private WeightHoursDao weighthoursDao;
    /**
     * 查询
     * @param weighthoursmodel
     * @return  设计工时列表
     */
    public List<WeightHoursModel> designHours(WeightHoursModel weighthoursmodel){
        return weighthoursDao.selectDesignHours(weighthoursmodel);
    }

    /**
     * 查询
     * @param weighthoursmodel
     * @return 设备重量列表
     */
    public List<WeightHoursModel> designWeight(WeightHoursModel weighthoursmodel){
        return weighthoursDao.selectDesignWeight(weighthoursmodel);
    }

    /**
     *保存工时、重量
     * @param design
     */
    public  int saveHoursWeight(WeightHoursModel design){
        User user = ShiroUtils.getUser();
        if(design.getDName() ==1){
            if (design.getDesId() == null){
                design.setDesInputUser(user.getName());
                design.setDesEditDate(new Date());
                design.setDfkId(design.getKbId());
                design.setDfkCode(design.getKbCode());
                design.setDInputId(design.getInputId());
                design.setDesDeleteFlag(0);
                design.setDesReceiveFlag(0);
                weighthoursDao.insertDesign(design);
            }else {
                weighthoursDao.updateDesign(design);
            }
            return design.getDName();
        }else {
            if (design.getWeiId() == null){
                design.setWeiInputUser(user.getName());
                design.setWeiEditDate(new Date());
                design.setWfkId(design.getKbId());
                design.setWfkCode(design.getKbCode());
                design.setWInputId(design.getInputId());
                design.setWeiDeleteFlag(0);
                design.setWeiReceiveFlag(0);
                weighthoursDao.insertEquipment(design);
            }else {
                weighthoursDao.updateEquipment(design);
            }
            return  design.getDName();
        }
    }

    /**查询工时单条*/
    public WeightHoursModel selectHoursOne(Integer desId){
        return weighthoursDao.selectHoursOne(desId);
    }

    /**查询重量单条*/
    public WeightHoursModel selectWeightOne(Integer weiId){
        return weighthoursDao.selectWeightOne(weiId);
    }

    /**删除工时数据*/
    public int delHouseOne(Integer desId){
        return weighthoursDao.upDelHours(desId, Constant.STATUS_NORMAL);
    }

    /**删除重量数据*/
    public int delWeightOne(Integer weiId){
        return weighthoursDao.upDelWeight(weiId,Constant.STATUS_NORMAL);
    }

    /**工时清空（全部）逻辑验证*/
    public int selDesHoursDel(WeightHoursModel weighthoursmodel){
        List<WeightHoursModel> weighthoursmodel1 = weighthoursDao.selectDesHoursDel1(weighthoursmodel);
        List<WeightHoursModel> weightHoursModel2 = weighthoursDao.selectDesHoursDel2(weighthoursmodel);
        if (weighthoursmodel1.size() == 0 && weightHoursModel2.size() > 0){
            return -1;
        }else if (weighthoursmodel1.size() == 0 && weightHoursModel2.size() == 0){
            return 0;
        }else if (weighthoursmodel1.size() < weightHoursModel2.size()){
            return -1;
        }else if (weighthoursmodel1.size() == weightHoursModel2.size()){
            return 1;
        }else {
            return 0;
        }
    }

    /**清空对应工时数据*/
    public int deleteDesHoursAll(WeightHoursModel weighthoursmodel){
        return weighthoursDao.deleteDesHours(weighthoursmodel);
    }

    /**重量清空（全部）逻辑验证*/
    public  int selDesWeightDel(WeightHoursModel weighthoursmodel){
        List<WeightHoursModel> weighthoursmodel3 = weighthoursDao.selectDesWeightDel3(weighthoursmodel);
        List<WeightHoursModel> weighthoursmodel4 = weighthoursDao.selectDesWeightDel4(weighthoursmodel);
        if (weighthoursmodel3.size() == 0 && weighthoursmodel4.size() > 0){
            return -1;
        }else if (weighthoursmodel3.size() == 0 && weighthoursmodel4.size() == 0){
            return 0;
        }else if (weighthoursmodel3.size() < weighthoursmodel4.size()){
            return -1;
        }else if (weighthoursmodel3.size() == weighthoursmodel4.size()){
            return 1;
        }else {
            return 0;
        }
    }

    /**清空对应重量数据*/
    public int deleteDesWeightAll(WeightHoursModel weighthoursmodel){
        return weighthoursDao.deleteDesWeight(weighthoursmodel);
    }
}